package com.linlong.linlongchat.handler;


import com.alibaba.fastjson.JSON;
import com.linlong.linlongchat.IMService;
import com.linlong.linlongchat.domain.po.Result;
import com.linlong.linlongchat.domain.websocket.data.Command;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ConnectionHandler {
    public static void execute(ChannelHandlerContext ctx, Command command) {
        if(IMService.USERS.containsKey(command.getTargetId())){
            ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.fail("该用户已上线，请重新登录"))));
            ctx.channel().disconnect();
            return;
        }

        IMService.USERS.put(command.getTargetId(), ctx.channel());
        ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(IMService.USERS.keySet())));
    }
}
